package com.septimus.portal.service;

import java.util.ArrayList;

import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.apache.log4j.Logger;

import com.septimus.portal.model.MenuPortal;

@Stateless
public class MenuRegistration {
	@Inject
	private Logger log;

	@Inject
	private EntityManager em;

	@Inject
	private Event<MenuPortal> menuEventSrc;

	public MenuPortal register(MenuPortal menu) throws Exception {
		log.info("\n\nRegistering Menu Item: " + menu.getModule().getName());
		MenuPortal menuPortal = em.merge(menu);
		menuEventSrc.fire(menu);
		return menuPortal;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<MenuPortal> loadAll() {
		return (ArrayList<MenuPortal>) em.createNativeQuery("SELECT * FROM TB_MENUPORTAL", MenuPortal.class)
				.getResultList();
	}
}
